﻿/*
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.Routing;
namespace Scaffold.Views
{
    
    public class ListPageBase<TItem> : ScaffoldViewPage<ListViewData<TItem>> 
    {
        public ListPageBase(ViewPageInfo viewInfo) : base(viewInfo)
        {

        }
        protected override void BuildContent(StringBuilder sb)
        {
            sb.AppendFormat("<h2>{0}</h2>\r\n", ViewInfo.Title);
            Type t = typeof(TItem);
            PropertyInfo[] properties = t.GetProperties();
            sb.Append("<table>");
            sb.Append("<tr>");
            foreach (PropertyInfo pi in properties)
            {
                sb.Append("<th>");
                sb.Append(pi.Name);
                sb.Append("</th>");
            }
            sb.Append("</tr>");

            foreach (TItem item in ViewData.Items)
            {
                sb.Append("<tr>");
                foreach (PropertyInfo pi in properties)
                {
                    sb.Append("<td>");
                    sb.Append(pi.GetValue(item, null));
                    sb.Append("</td>");
                }
                sb.Append("</tr>");
            }
            
            sb.Append("</table>");
            RouteValueDictionary firstDict = new RouteValueDictionary();
            RouteValueDictionary prevDict = new RouteValueDictionary();
            RouteValueDictionary nextDict = new RouteValueDictionary();
            RouteValueDictionary lastDict = new RouteValueDictionary();


            firstDict.Add("PageIndex", 0);
            firstDict.Add("PageSize", ViewData.PageSize);

            prevDict.Add("PageIndex", ViewData.PageIndex - 1);
            prevDict.Add("PageSize", ViewData.PageSize);

            nextDict.Add("PageIndex", ViewData.PageIndex + 1);
            nextDict.Add("PageSize", ViewData.PageSize);

            lastDict.Add("PageIndex", ViewData.PageIndex + 1);
            lastDict.Add("PageSize", ViewData.PageSize);
            sb.Append("<p>");
            if (ViewData.PageIndex != 0)
            {
                sb.Append(Html.ActionLink("|<<", "List", ViewInfo.Title, firstDict));
                sb.Append("&nbsp;");
                sb.Append(Html.ActionLink("<<", "List", ViewInfo.Title, prevDict));
            }
            else
            {
                sb.Append("|&lt;&lt;&nbsp;&lt;&lt;");
            }

            sb.AppendFormat("&nbsp;Items {0} - {1} of {2}&nbsp;", ViewData.FirstItem, ViewData.LastItem, ViewData.ItemCount);

            if (ViewData.PageIndex != ViewData.PageCount - 1)
            {
                sb.Append(Html.ActionLink(">>", "List", ViewInfo.Title, nextDict));
                sb.Append("&nbsp;");
                sb.Append(Html.ActionLink(">>|", "List", ViewInfo.Title, lastDict));
            }
            else
            {
                sb.Append("&gt;&gt;&nbsp;&gt;&gt;|");    
            }
            sb.Append("</p>");
        }
    }
}
